﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;

namespace THenu.WMI.IPConfiguration
{
    static class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
        /// <summary> 
        /// 得到当前活动的窗口 
        /// </summary> 
        /// <returns></returns> 
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        /// <summary>
        /// 设置此窗体为活动窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        //寻找符合条件的窗口
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            using (Mutex mutexLock = FrmMain.MutexLock)
            {
                if (!mutexLock.SafeWaitHandle.IsClosed && !FrmMain.CreateNew)
                {
                    // 根据标题查找窗体
                    IntPtr handle = FindWindow(null, " IP 切换助手");
                    // 设置置顶窗口
                    // SetWindowPos(handle, -1, 0, 0, 0, 0, 1 | 2);
                    SetForegroundWindow(handle);
                }
                else
                {
                    Application.Run(new FrmMain());
                }
            }
        }
    }
}
